require_dependency File.expand_path(File.dirname(__FILE__) + '/../../vendor/plugins/sourdough/app/controllers/users_controller')

class UsersController < ApplicationController

  def new
    @user = User.new
    @user.jobs.build
  end

  def create
    @user = User.new(params[:user])
    @user.jobs.build(params[:job].merge(:user => @user))

    respond_to do |format|
      if @user.valid? and @user.jobs.any?(&:valid?) and @user.save
        flash[:notice] = "Thank you for signing up!  We just sent you a confirmation email to #{@user.email}.  Please click on the activation link in the email to complete your sign up."
        UserMailer.deliver_signup_notification(@user, request.protocol + request.host_with_port + activate_path(@user.activation_code))
        format.html { redirect_to create_redirection_path }
        format.xml  { head :created, :location => home_path }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @user.errors.to_xml }
      end
    end
  end

  def upload_vitae
    @document = Document.new(params[:document])
    current_user.document = @document

    respond_to do |format|
      if @document.valid? and current_user.valid? and current_user.save
        flash[:notice] = 'Your vitae has been uploaded.'
        format.html { redirect_to vitae_user_url(current_user) }
      else
        format.html { render :action => "vitae" }
      end
    end
  end

  def delete_vitae
    @document = current_user.document
    @document.destroy

    respond_to do |format|
      flash[:notice] = 'Your vitae has been deleted'
      format.html { redirect_to vitae_user_url(current_user) }
    end
  end

  private

  def activation_redirection_path
    personal_user_path(current_user)
  end

  def update_redirection_path(user)
    case params['commit']
    when 'Change Password'
      return password_user_url(current_user)
    when 'Update Email Address'
      return  edit_user_url(current_user)
    when 'Update Additional Information'
      return additional_information_user_url(current_user)
    end
    return personal_user_url(current_user)
  end

  def update_render_action
    case params['commit']
    when 'Change Password'
      return 'password'
    when 'Update Personal Information'
      return 'personal'
    end
    return 'edit'
  end

end
